namespace PlexMediaLinker
{
    // 简易的添加/编辑组对话框
    public class GroupDialog : Form
    {
        public GroupConfig Config { get; private set; }
        private TextBox txtName;
        private Label lblSrc, lblDst;
        private CheckBox chkRemove;
        private RadioButton rbHardLink, rbCopy;
        private RadioButton rbFlatten, rbPreserve;
        private RadioButton rbMediaOnly, rbAllFiles;

        public GroupDialog()
        {
            Text = "添加/编辑监控组";
            ClientSize = new Size(500, 350);
            
            txtName = new TextBox { PlaceholderText = "组名（可留空使用默认）", Dock = DockStyle.Top };
            
            // 同步模式选择
            var modePanel = new FlowLayoutPanel { Dock = DockStyle.Top, Height = 30 };
            var lblMode = new Label { Text = "同步模式:", AutoSize = true };
            rbHardLink = new RadioButton { Text = "硬链接", Checked = true, AutoSize = true };
            rbCopy = new RadioButton { Text = "复制同步", AutoSize = true };
            modePanel.Controls.AddRange([lblMode, rbHardLink, rbCopy]);
            
            // 目录结构选择
            var structPanel = new FlowLayoutPanel { Dock = DockStyle.Top, Height = 30 };
            var lblStruct = new Label { Text = "目录结构:", AutoSize = true };
            rbFlatten = new RadioButton { Text = "扁平化", Checked = true, AutoSize = true };
            rbPreserve = new RadioButton { Text = "保持结构", AutoSize = true };
            structPanel.Controls.AddRange([lblStruct, rbFlatten, rbPreserve]);
            
            // 文件类型过滤选择
            var filterPanel = new FlowLayoutPanel { Dock = DockStyle.Top, Height = 30 };
            var lblFilter = new Label { Text = "文件类型:", AutoSize = true };
            rbMediaOnly = new RadioButton { Text = "仅媒体/字幕", Checked = true, AutoSize = true };
            rbAllFiles = new RadioButton { Text = "所有文件", AutoSize = true };
            filterPanel.Controls.AddRange([lblFilter, rbMediaOnly, rbAllFiles]);
            
            var btnSrc = new Button { Text = "选择源目录", Dock = DockStyle.Top };
            lblSrc = new Label { Text = "未选择", Dock = DockStyle.Top };
            var btnDst = new Button { Text = "选择目标目录", Dock = DockStyle.Top };
            lblDst = new Label { Text = "未选择", Dock = DockStyle.Top };
            chkRemove = new CheckBox { Text = "删除不存在源的文件", Dock = DockStyle.Top };
            var btnOk = new Button { Text = "确定", Dock = DockStyle.Bottom };
            
            btnSrc.Click += (s, e) => { var f = new FolderBrowserDialog(); if (f.ShowDialog() == DialogResult.OK) lblSrc.Text = f.SelectedPath; };
            btnDst.Click += (s, e) => { var f = new FolderBrowserDialog(); if (f.ShowDialog() == DialogResult.OK) lblDst.Text = f.SelectedPath; };
            btnOk.Click += (s, e) =>
            {
                Config = new GroupConfig
                {
                    Name = txtName.Text,
                    Source = lblSrc.Text,
                    Target = lblDst.Text,
                    RemoveMissing = chkRemove.Checked,
                    Mode = rbHardLink.Checked ? SyncMode.HardLink : SyncMode.Copy,
                    Structure = rbFlatten.Checked ? StructureMode.Flatten : StructureMode.Preserve,
                    FileFilter = rbMediaOnly.Checked ? FileFilterMode.MediaOnly : FileFilterMode.AllFiles
                };
                DialogResult = DialogResult.OK;
                Close();
            };
            
            Controls.AddRange([chkRemove, lblDst, btnDst, lblSrc, btnSrc, filterPanel, structPanel, modePanel, txtName, btnOk]);
        }
    }

}
